package mercurius.fitness.client.rb.user;

import java.util.Date;
import java.util.HashMap;
import java.util.Vector;

import mercurius.fitness.client.Fitness;
import mercurius.fitness.client.util.MercuriusHelper;
import mercurius.fitness.client.util.AbstractResponseCommand;
import net.pleso.framework.client.dal.SelectParams;
import net.pleso.framework.client.util.DateUtil;

import com.google.gwt.user.client.rpc.AsyncCallback;

public class UserDataSet {
	public UserDataSet() {
	
	}

	public void select(final SelectParams params, final AsyncCallback callback) {
		
		AbstractResponseCommand pass = new AbstractResponseCommand() {
			
			   public void execute() {
				   
			   }

			public Object getObject() {
				return params;
			}

			public void setObject(Object obj) {
				Vector props = (Vector) obj;
				User[] users = new User[props.size()];
				for(int i=0;i<props.size();i++) {
					HashMap user = (HashMap)props.get(i);
					users[i] = new User(
							
							new Integer(Integer.parseInt((String) user.get("id"))),
							(String) user.get("user"),
							(String) user.get("firstname"),
							(String) user.get("lastname"),
							DateUtil.parseDate((String) user.get("dob")),
							new Integer(Integer.parseInt((String) user.get("hr_max"))),
							new Float(Float.parseFloat((String) user.get("vo2_max"))),
							new Integer(Integer.parseInt((String) user.get("gender"))),
							new Float(Float.parseFloat((String) user.get("height"))),
							(String) user.get("height_unit"),
							(String) user.get("feedkey"),
							(String) user.get("adminfeedkey")
							
					);
				}
				callback.onSuccess(users);
			}
			 };

			MercuriusHelper.updateMercurius(new User(),MercuriusHelper.MERCURIUS_SELECT,callback,pass);
	}
	
	public void selectCount(AsyncCallback callback) {
	//	MercuriusHelper.updateMercurius(new User(),MercuriusHelper.MERCURIUS_SELECT_COUNT,callback);
		callback.onSuccess(new Integer(1));
	}
	
	public void add(final User user, AsyncCallback callback) {
	}
	
	public void update(final User user, AsyncCallback callback) {
		
		AbstractResponseCommand pass = new AbstractResponseCommand() {
			
			   public void execute() {
				   if(Fitness.get().getExternalProperty("settings").equals("0")) {
					   Fitness.get().setExternalProperty("settings","1");
					   Fitness.get().load();
				   }
			   }

			public void setObject(Object obj) {
				
			}

			public Object getObject() {
				return null;
			}
			 };
		
		MercuriusHelper.updateMercurius(user,MercuriusHelper.MERCURIUS_UPDATE,callback,pass);
	}
	
	public void get(final Integer id, final AsyncCallback callback)
	{
		
		AbstractResponseCommand pass = new AbstractResponseCommand() {
			
			   public void execute() {

			   }

			public Object getObject() {
				return id;
			}

			public void setObject(Object obj) {
				
				
				HashMap user = (HashMap) obj;
				Date dob = new Date();
				if(((String) user.get("dob")).length() > 0) {
					dob = DateUtil.parseDate((String) user.get("dob"));
				}
				User users = new User(
						
						new Integer(Integer.parseInt((String) user.get("id"))),
						(String) user.get("user"),
						(String) user.get("firstname"),
						(String) user.get("lastname"),
						dob,
						new Integer(Integer.parseInt((String) user.get("hr_max"))),
						new Float(Float.parseFloat((String) user.get("vo2_max"))),
						new Integer(Integer.parseInt((String) user.get("gender"))),
						new Float(Float.parseFloat((String) user.get("height"))),
						(String) user.get("height_unit"),
						(String) user.get("feedkey"),
						(String) user.get("adminfeedkey")
						
				);

				callback.onSuccess(users);
			}
			 };
			 
			 MercuriusHelper.updateMercurius(new User(),MercuriusHelper.MERCURIUS_SELECT_BY_ID,callback,pass);

	}
	
	public void delete(Integer id, AsyncCallback callback)
	{
	}
}
